﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormBookmarked : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains("fguid"))
            {
                Response.Redirect("Login.aspx?prev=Bookmarked.aspx");
            }

            if (!IsPostBack)
            {
                GetBookmarkedGames();
            }
        }

        private void GetBookmarkedGames()
        {
            string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
            long userId = Convert.ToInt64(splitStr[0]);

            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridGames.DataSource = dataContext.GetBookmarkedGames(userId);
                GridGames.DataBind();
            }
        }

        protected void lnkGamesPlay_Click(object sender, EventArgs e)
        {
            LinkButton lnkGame = sender as LinkButton;
            int gameId = Convert.ToInt32(lnkGame.Attributes["GameID"]);
            string title = lnkGame.Text;
            string imageUrl = "";

            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                imageUrl = dataContext.GetGameImage(gameId);
            }

            string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
            long userId = Convert.ToInt64(splitStr[0]);
            string username = splitStr[1];
            Common.AddMemberActivity(username, userId, title, gameId, imageUrl, ActivityType.GamePlaying);

            Response.Redirect(string.Format("PlayGame.aspx?gameid={0}", gameId));
        }

        protected void imgRemove_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btnRemove = sender as ImageButton;
            int gameId = Convert.ToInt32(btnRemove.Attributes["GameID"]);
            string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
            long userId = Convert.ToInt64(splitStr[0]);

            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                dataContext.RemoveBookmark(userId, gameId);
            }

            GetBookmarkedGames();
        }
    }
}